/**
 * user edit
 */
layui.use(['form','tableSelect', 'layedit', 'laydate'], function () {
    let form = layui.form,
        layer = layui.layer,
        laydate = layui.laydate,
        tableSelect = layui.tableSelect,
        $ = layui.jquery;

    laydate.render({
        elem: '#birthday'
    });
    let u=localStorage.getItem("a@m");
    let loginU={};
    if(u==undefined){
        //layer.msg('请登录', {icon: 1, time: 500},function () {window.location = 'page/login.html';});
    }else{
        loginU=decryptLocalStorage(u);
    }
    let _data=localStorage.getItem("sportData");
    let viewData=undefined;
    let provinceId='',cityId='',districtId='',schoolId='';
    let provinceName='',cityName='',districtName='',schoolName='';
    if(_data!=undefined){
        viewData=decryptLocalStorage(_data,loginU.token);
        provinceId=viewData.provinceId;
        cityId=viewData.cityId;
        districtId=viewData.districtId;
        schoolId=viewData.schoolId;
        provinceName=viewData.provinceName;
        cityName==viewData.cityName;
        districtName=viewData.districtName;
        schoolName=viewData.schoolName;

        $("#userName").val(viewData.userName);
        $("#password").val(viewData.password);
        $("#trueName").val(viewData.trueName);
        $("#birthday").val("");
        $("#mobile").val(viewData.mobile);
        $("#remark").val(viewData.remark);
        $('input[name="userName"]').prop('disabled','disabled');
        if(viewData.sex=='男'){
            $('input[name="userType"][value="男"]').prop('checked','checked');
        }else{
            $('input[name="userType"][value="女"]').prop('checked','checked');
        }
        if(viewData.userType=='system'){
            $('input[name="userType"][value="system"]').prop('checked','checked');
            $('.power-panel').hide();
            $('.power-area-panel').hide();
        }else if(viewData.userType=='nation'){
            $('input[name="userType"][value="nation"]').prop('checked','checked');
            $('.power-panel').hide();
            $('.power-area-panel').hide();
        }else if(viewData.userType=='province'){
            $('input[name="userType"][value="province"]').prop('checked','checked');
            $($('.power-panel')[0]).show();
            $($('.power-panel')[1]).hide();
            $($('.power-area-panel')[0]).show();
            $($('.power-area-panel')[1]).hide();
            $($('.power-area-panel')[2]).hide();
            $($('.power-area-panel')[3]).hide();
        }else if(viewData.userType=='city'){
            $('input[name="userType"][value="city"]').prop('checked','checked');
            $($('.power-panel')[0]).show();
            $($('.power-area-panel')[0]).show();
            $($('.power-area-panel')[1]).show();
            $($('.power-area-panel')[2]).hide();
            $($('.power-area-panel')[3]).hide();
        }else if(viewData.userType=='district'){
            $('input[name="userType"][value="district"]').prop('checked','checked');
            $('.power-panel').show();
            $($('.power-area-panel')[0]).show();
            $($('.power-area-panel')[1]).show();
            $($('.power-area-panel')[2]).show();
            $($('.power-area-panel')[3]).hide();
        }else if(viewData.userType=='school'){
            $('input[name="userType"][value="school"]').prop('checked','checked');
            $('.power-panel').show();
            $('.power-area-panel').show();
        }else if(viewData.userType=='teacher'){
            $('input[name="userType"][value="teacher"]').prop('checked','checked');
            $('.power-panel').show();
            $('.power-area-panel').show();
        }
        form.render('radio');
    }

    form.on('radio(userType)', function (data) {
        let userType=data.value;
        if(userType=='system'){
            $('.power-panel').hide();
            $('.power-area-panel').hide();
        }else if(userType=='nation'){
            $('.power-panel').hide();
            $('.power-area-panel').hide();
        }else if(userType=='province'){
            $($('.power-panel')[0]).show();
            $($('.power-panel')[1]).hide();
            $($('.power-area-panel')[0]).show();
            $($('.power-area-panel')[1]).hide();
            $($('.power-area-panel')[2]).hide();
            $($('.power-area-panel')[3]).hide();
        }else if(userType=='city'){
            $($('.power-panel')[0]).show();
            $($('.power-area-panel')[0]).show();
            $($('.power-area-panel')[1]).show();
            $($('.power-area-panel')[2]).hide();
            $($('.power-area-panel')[3]).hide();
        }else if(userType=='district'){
            $('.power-panel').show();
            $($('.power-area-panel')[0]).show();
            $($('.power-area-panel')[1]).show();
            $($('.power-area-panel')[2]).show();
            $($('.power-area-panel')[3]).hide();
        }else if(userType=='school'){
            $('.power-panel').show();
            $('.power-area-panel').show();
        }else if(userType=='teacher'){
            $('.power-panel').show();
            $('.power-area-panel').show();
        }
    });

    provinceList(form,loginU.token,provinceId,cityId,districtId,schoolId);
    form.on('select(province)', function (data) {
        provinceId = data.value;
        if(provinceId!=undefined){
            $("#district").html("<option value=''>请选择区</option>");
            $("#school").html("<option value=''>请选择学校</option>");
            cityList(form,loginU.token,provinceId,cityId,districtId,schoolId);
        }
    });

    form.on('select(city)', function (data) {
        cityId = data.value;
        if(cityId!=undefined){
            $("#school").html("<option value=''>请选择学校</option>");
            districtList(form,loginU.token,provinceId,cityId,districtId,schoolId);
        }
    });

    form.on('select(district)', function (data) {
        districtId = data.value;
        if(districtId!=undefined){
            schoolList(form,loginU.token,provinceId,cityId,districtId,schoolId);
        }
    });

    form.on('select(school)', function (data) {
        schoolId = data.value;
    });

    //监听提交
    form.on('submit(saveBtn)', function (data) {
        let params=data.field;
        params.token=loginU.token;
        if(params.userType=='system'){
            params.provinceId=0;
            params.cityId=0;
            params.districtId=0;
            params.schoolId=0;
        }else if(params.userType=='nation'){
            params.provinceId=0;
            params.cityId=0;
            params.districtId=0;
            params.schoolId=0;
        }else if(params.userType=='province'){
            if(provinceId=='')
            {
                layer.msg('请选择省份/直辖市', {
                    icon: 2,
                    time: 2000
                });
                return;
            }
            params.provinceId=provinceId;
            params.cityId=0;
            params.districtId=0;
            params.schoolId=0;
        }else if(params.userType=='city'){
            if(provinceId=='')
            {
                layer.msg('请选择省份/直辖市', {
                    icon: 2,
                    time: 2000
                });
                return;
            }
            if(cityId=='')
            {
                layer.msg('请选择市', {
                    icon: 2,
                    time: 2000
                });
                return;
            }
            params.provinceId=provinceId;
            params.cityId=cityId;
            params.districtId=0;
            params.schoolId=0;
        }else if(params.userType=='district'){
            params.provinceId=provinceId;
            params.cityId=cityId;
            params.districtId=districtId;
            params.schoolId=0;
        }else if(params.userType=='school'){
            if(provinceId=='')
            {
                layer.msg('请选择省份/直辖市', {
                    icon: 2,
                    time: 2000
                });
                return;
            }
            if(cityId=='')
            {
                layer.msg('请选择市', {
                    icon: 2,
                    time: 2000
                });
                return;
            }
            if(districtId=='')
            {
                layer.msg('请选择区', {
                    icon: 2,
                    time: 2000
                });
                return;
            }
            if(schoolId=='')
            {
                layer.msg('请选择学校', {
                    icon: 2,
                    time: 2000
                });
                return;
            }
            params.provinceId=provinceId;
            params.cityId=cityId;
            params.districtId=districtId;
            params.schoolId=schoolId;
        }else if(params.userType=='teacher'){
            if(provinceId=='')
            {
                layer.msg('请选择省份/直辖市', {
                    icon: 2,
                    time: 2000
                });
                return;
            }
            if(cityId=='')
            {
                layer.msg('请选择市', {
                    icon: 2,
                    time: 2000
                });
                return;
            }
            if(districtId=='')
            {
                layer.msg('请选择区', {
                    icon: 2,
                    time: 2000
                });
                return;
            }
            if(schoolId=='')
            {
                layer.msg('请选择学校', {
                    icon: 2,
                    time: 2000
                });
                return;
            }
            params.provinceId=provinceId;
            params.cityId=cityId;
            params.districtId=districtId;
            params.schoolId=schoolId;
        }
        //添加
        if(viewData==undefined){
            request.add('user',JSON.stringify(params),function(result){
                if(result.code==0){
                    layer.msg('保存成功', {
                        icon: 1,
                        time: 500
                    },function () {
                        // 关闭弹出层
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    });

                }else{
                    layer.msg(result.msg || '保存失败', {
                        icon: 2,
                        time: 2000
                    });
                }
            });
        }
        //修改
        else{
            params.userId=viewData.userId;
            request.update('user',JSON.stringify(params),function(result){
                if(result.code==0){
                    layer.msg('保存成功', {
                        icon: 1,
                        time: 500
                    },function () {
                        // 关闭弹出层
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    });

                }else{
                    layer.msg(result.msg || '保存失败', {
                        icon: 2,
                        time: 2000
                    });
                }
            });
        }

        return false;
    });

});